package demo1;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther: 杨通通
 * @Date: 2024/9/10 23:18
 * @Description:
 */
public class Stream1 {

    public static void main(String[] args) {
        List<Person> list = Arrays.asList(new Person("ytt1"), new Person("ytt2"));
        //输入类型和输出都是Person所以Person::updateName 类方法可以当参数
        list.replaceAll(Person::updateName);

        System.out.println(list);

        list.replaceAll((p)->{
//            System.out.println(p);
            p.setName(p.getName()+"嘻嘻");
            return p;
        });

        list.forEach(System.out::println);

        List<String> collect = list.stream().map(Person::getName).limit(1).collect(Collectors.toList());
        System.out.println(collect.size());

        Person ytt1 = new Person("ytt1", 12);
        Person ytt2 = new Person("ytt1", 12);
        Person ytt3 = new Person("ytt1", 14);
        Person ytt4 = new Person("ytt2", 12);
        System.out.println(ytt1== ytt2);//判断地址是否相同 false
        System.out.println(ytt1.equals(ytt3));  //true
        System.out.println(ytt1.equals(ytt4));  //false

        List<Person> list2 = Arrays.asList(ytt1, ytt2, ytt3, ytt4);
        List<Person> collect1 = list2.stream().distinct().collect(Collectors.toList());//去重，只要name相等即是重复
        System.out.println(collect1);//[Person{name='ytt1', age=12}, Person{name='ytt2', age=12}]

    }
}

class Person{
    private String name;
    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Person updateName(){
        this.setName(getName()+" update");
        return this;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public boolean equals(Object o) {
        if(this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person p = (Person) o;
        return p.getName() == this.getName();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}